package com.wm.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Scriptorium;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.CollectService;
import com.wm.service.RestaurantScriptoriumConService;
import com.wm.service.RestaurantService;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class MobileResListAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(MobileResListAction.class);
	
	private RestaurantService restaurantService;
	private RestaurantScriptoriumConService restaurantScriptoriumConService;
	private ScriptoriumService scriptoriumService;

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}

	public RestaurantScriptoriumConService getRestaurantScriptoriumConService() {
		return restaurantScriptoriumConService;
	}

	public void setRestaurantScriptoriumConService(
			RestaurantScriptoriumConService restaurantScriptoriumConService) {
		this.restaurantScriptoriumConService = restaurantScriptoriumConService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	private Map session;
	
	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private long id;
	private List list;
	private double pointX;
	private double pointY;

	public double getPointX() {
		return pointX;
	}

	public void setPointX(double pointX) {
		this.pointX = pointX;
	}

	public double getPointY() {
		return pointY;
	}

	public void setPointY(double pointY) {
		this.pointY = pointY;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		if(this.getPointX()==0 && this.getPointY()==0){
			//查cookie记录
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			UserCookie userCookie = new UserCookie(request,response);
			String x = userCookie.getCookie(Constant.POINTX_COOKIE_KEY);
			String y = userCookie.getCookie(Constant.POINTY_COOKIE_KEY);
			if(x!=null&&!x.equals("")&&y!=null&&!y.equals("")){
				this.setPointX(Double.parseDouble(x));
				this.setPointY(Double.parseDouble(y));
				//查询饭店
				this.setList(this.getRestaurantService().findRestaurantByPointXPointYValidIsShow(pointX, pointY, 1, 1));
			}else{
				return "indexAction";
			}
		}else{
			//查询饭店
			this.setList(this.getRestaurantService().findRestaurantByPointXPointYValidIsShow(pointX, pointY, 1, 1));
			//记录cookie
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			UserCookie userCookie = new UserCookie(request,response);
			String x = userCookie.getCookie(Constant.POINTX_COOKIE_KEY);
			String y = userCookie.getCookie(Constant.POINTY_COOKIE_KEY);
			if(x!=null&&!x.equals("")&&y!=null&&!y.equals("")){
				userCookie.removeCookie(Constant.POINTX_COOKIE_KEY);
				userCookie.removeCookie(Constant.POINTY_COOKIE_KEY);
			}
			userCookie.addCookie(Constant.POINTX_COOKIE_KEY, String.valueOf(pointX));
			userCookie.addCookie(Constant.POINTY_COOKIE_KEY, String.valueOf(pointY));
		}
		return "index";
	}
	
}
